package com.nightsoul.groovy

public class AdvancedParameterUsage{

	public static void main(def args){
		def usage = new AdvancedParameterUsage();
		println usage.sumWithDefaults(1, 1);
		println usage.sumWithDefaults(1, 1, 1);
		
		println usage.sumWithList([1, 1]);
		println usage.sumWithList([1, 1, 1]);
		
		println usage.sumWithOptionals(1, 1);
		println usage.sumWithOptionals(1, 1, 1);
		
		println usage.sumNamed([a:1, b:1]);
		println usage.sumNamed([a:1, b:1, c:1]);
		println usage.sumNamed([c:1]);
	}
	
	
	def sumWithDefaults(a, b, c=0) {
		return a + b + c;
	}
	
	def sumWithList(List list) {
		return list.inject(0) { sum, i ->
			sum+= i;
		}
	}
	
	def sumWithOptionals(a, b, Object[] optionals) {
		return a + b + sumWithList(optionals.toList());
	}
	
	
	def sumNamed(Map map) {
		['a', 'b', 'c'].each {
			map.get(it, 0)
		};
		return map.a + map.b + map.c;
	}
	
	
}
